package com.jin.blog.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.jin.blog.core.BlogConstants;
import com.jin.blog.pojo.Article;

public class TagController extends BaseController {
	public void index() {
		String tag = null;
		try {
			tag = URLDecoder.decode(getPara(0),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Integer pageNum = getParaToInt("p", 1);
		Page<Article> page = Article.dao.paginateByCache("article", "tag_" + tag + "_" + pageNum, pageNum, BlogConstants.PAGE_SIZE, 
				"select * ", 
				"from article where finish = 1 and tags like ? order by id desc", 
				"%" + tag + "%"); 
		setAttr("page_news", page);
		
		List<Map<String, Object>>crumbs = new ArrayList<>();
		Map<String, Object>map = new HashMap<>();
		map.put("name", tag);
		crumbs.add(map);
		setAttr("crumbs", crumbs);
		
		getCommonData();
		render("index.html");
	}
}
